package huu.phong.googlemusic;

import huu.phong.googlemusic.model.Song;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;

public class SongListRenderer extends DefaultListCellRenderer {
	private static final long serialVersionUID = 1118681553924124042L;
	ImageIcon first = new ImageIcon(SongListRenderer.class.getResource("/images/song.png"));
	ImageIcon last = new ImageIcon(SongListRenderer.class.getResource("/images/rating16.png"));
	ImageIcon disableLast = new ImageIcon(SongListRenderer.class.getResource("/images/rating16_disable.png"));
	Color odd = new Color(238, 238, 238);
	Color even = Color.WHITE;
	Color selected = new Color(51, 153, 255);
	
	public SongListRenderer(){
	}
	
	private String numberToString(int number){
		String ret = String.valueOf(number);
		while (ret.length() < 2){
			ret = "0" + ret;
		}
		return ret;
	}
	
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		JLabel both = new JLabel();
		both.setOpaque(true);
		both.setLayout(new BorderLayout());
		JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, false);
		label.setOpaque(false);
		JLabel hq = null;
		JLabel number = new JLabel(" " + numberToString(index + 1) + ".");
		number.setForeground(Color.BLUE);
		Song song = (Song) value;
		if (song.getRating() > 0){
			hq = new JLabel(last);
		}else{
			hq = new JLabel(disableLast);
		}
		hq.setText(" Lượt nghe: " + song.getPlayCount() + " ");
		hq.setForeground(Color.BLUE);
		if (index % 2 == 1){
			both.setBackground(odd);
		}else{
			both.setBackground(even);
		}
		if (isSelected) {
			both.setBackground(selected);
			number.setForeground(Color.WHITE);
			hq.setForeground(Color.WHITE);
		}
		label.setIcon(first);
		both.add(number, BorderLayout.WEST);
		both.add(label, BorderLayout.CENTER);
		both.add(hq, BorderLayout.EAST);
		return both;
	}
}
